package com.py.warehouse.service.impl;

import java.util.List;

import com.py.warehouse.domain.WhInventory;
import com.py.warehouse.mapper.BsWarehouseMapper;
import com.py.warehouse.mapper.WhInventoryMapper;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.py.warehouse.mapper.WhCheckDetailsMapper;
import com.py.warehouse.domain.WhCheckDetails;
import com.py.warehouse.service.IWhCheckDetailsService;

@Service
public class WhCheckDetailsServiceImpl implements IWhCheckDetailsService {

    @Autowired
    private WhCheckDetailsMapper whCheckDetailsMapper;

    @Autowired
    private WhInventoryMapper whInventoryMapper;

    @Autowired
    private BsWarehouseMapper bsWarehouseMapper;

    /**
     * 查询ee
     *
     * @param detailsId ee主键
     * @return ee
     */
    @Override
    public WhCheckDetails selectWhCheckDetailsByDetailsId(Long detailsId)
    {
        return whCheckDetailsMapper.selectWhCheckDetailsByDetailsId(detailsId);
    }

    /**
     * 查询ee列表
     *
     * @param whCheckDetails ee
     * @return ee
     */
    @Override
    public List<WhCheckDetails> selectWhCheckDetailsList(WhCheckDetails whCheckDetails)
    {
        return whCheckDetailsMapper.selectWhCheckDetailsList(whCheckDetails);
    }

    /**
     * 新增ee
     *
     * @param whCheckDetails ee
     * @return 结果
     */
    @Override
    public int insertWhCheckDetails(WhCheckDetails whCheckDetails)
    {
        //转正数
        Long preparation = Math.abs(whCheckDetails.getDetailsLossNumber());
        whCheckDetails.setCreateBy(SecurityUtils.getUsername());
        whCheckDetails.setCreateTime(DateUtils.getNowDate());
        whCheckDetails.setPreparation(preparation);

        int result =  whCheckDetailsMapper.insertWhCheckDetails(whCheckDetails);
        if (result > 0){
            //修改待出入库数量
            whInventoryMapper.updatePendingInNum(whCheckDetails);

            WhInventory whInventory = whInventoryMapper.selectInventoryById(whCheckDetails.getInventoryQueryId());
            System.out.println("库存表");
            System.out.println(whInventory);
            bsWarehouseMapper.updateWarehouseLock(whInventory.getWarehouseCode());
        }
        return result;
    }

    /**
     * 修改ee
     *
     * @param checkCode ee
     * @return 结果
     */
    @Override
    public int updateWhCheckDetails(String checkCode)
    {
        int result = whCheckDetailsMapper.updateWhCheckDetails(checkCode);

        List<WhCheckDetails> list = whCheckDetailsMapper.selectDetailsByWarehouseCode(checkCode);

        for (WhCheckDetails whCheckDetails : list) {
            whInventoryMapper.updatePendingInNumJian(whCheckDetails);
        }
        return result;
    }


}
